//
//  UIBarButtonItem+Extension.swift
//  sinaweibo
//
//  Created by lst on 16/5/20.
//  Copyright © 2016年 lst. All rights reserved.
//

import UIKit
extension UIBarButtonItem{
    convenience init(imageName : String? = nil, title: String? = nil, target: AnyObject?, action: Selector) {
        self.init()
        let btn = UIButton()
        
        //设置图片
        if let image = imageName {
            
            btn.setImage(UIImage(named: image), forState: .Normal)
            btn.setImage(UIImage(named: image + "_highlighted"), forState: .Highlighted)

        }
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        btn.setTitle(title, forState: .Normal)
        //设置图片的显示范围
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0)
        //设置颜色
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //设置自适应大小
        btn.sizeToFit()
        //设置自定义视图
        self.customView = btn
        
    }
    
    
    
}
